﻿
CREATE PROCEDURE sp_User_Update 
	-- Add the parameters for the stored procedure here
	(
				@UserKey int,
				@UserLoginID varchar(50),
				@UserPassword varchar(50),
				@UserFirstName varchar(50),
				@UserLastName varchar(50),
				@UserPrimaryEmailID varchar(150),
				@UserVerificationCode varchar(50),
				@UserIsVarified bit = false,
				@Sys_ModifiedByID varchar(50) = NULL,
				@Sys_DeleteType int = 0,
				@Sys_IsDeleted bit = false,
				@Sys_OrganizationID int = -1,
				@Sys_InitialPrimaryKeys varchar(100)= NULL,
				@Sys_WorkstationName varchar(50)= NULL,
				@ConcurrencyTimeStamp datetime = NULL,
				@Sys_DateOfModification datetime OUTPUT
	)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	IF @ConcurrencyTimeStamp = NULL
	BEGIN
		RAISERROR('Missing @ConcurrencyTimeStamp', 11,1);
	END
	DECLARE @CurrentModificationDate datetime;
	SET @CurrentModificationDate = (SELECT [Sys_DateOfModification] FROM [User] WHERE [UserKey] = @UserKey);
	
	If @ConcurrencyTimeStamp != @CurrentModificationDate
	BEGIN
		RAISERROR('Record is Changed by another process', 11,1);
	END
	
	IF @Sys_WorkstationName = 'Default'	
	BEGIN
		SET @Sys_WorkstationName = (SELECT @@SERVERNAME);
	END
	SET @Sys_DateOfModification = GETDATE();
	
    UPDATE	[User]
	SET 
			[UserLoginID] = @UserLoginID,
			[UserPassword] = @UserPassword,
			[UserFirstName] = @UserFirstName,
			[UserLastName] = @UserLastName,
			[UserPrimaryEmailID] = @UserPrimaryEmailID,
			[UserVerificationCode] = @UserVerificationCode,
			[UserIsVarified] = @UserIsVarified,
			[Sys_DateOfModification] = @Sys_DateOfModification,
			[Sys_ModifiedByID] = @Sys_ModifiedByID,
			[Sys_DeleteType] = @Sys_DeleteType,
			[Sys_IsDeleted] = @Sys_IsDeleted,
			[Sys_OrganizationID] = @Sys_OrganizationID,
			[Sys_InitialPrimaryKeys] = @Sys_InitialPrimaryKeys,
			[Sys_WorkstationName] = @Sys_WorkstationName
	 WHERE
		(
			[UserKey] = @UserKey
		)

END
